package com.cwd.arm.infrastructure.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cwd.arm.infrastructure.dao.ProjectDao;
import com.cwd.arm.infrastructure.dao.base.DaoImpl;
import com.cwd.arm.infrastructure.mapper.ProjectMapper;
import com.cwd.arm.infrastructure.po.ProjectPo;
import com.cwd.arm.infrastructure.query.ProjectDaoQuery;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * Description:项目
 * Created on 2021年11月16日
 *
 * @author yan
 */
@Service
public class ProjectDaoImpl extends DaoImpl<ProjectMapper, ProjectPo> implements ProjectDao {

    @Override
    public Page<ProjectPo> selectPage(ProjectDaoQuery daoQuery) {
        Page<ProjectPo> poPage = new Page<>(daoQuery.getCurrentPage(),daoQuery.getPageSize());//参数一是当前页，参数二是每页个数
        QueryWrapper<ProjectPo> queryWrapper = new QueryWrapper<>();
        setProjectQueryWrapper(daoQuery, queryWrapper);
        poPage = baseMapper.selectPage(poPage, queryWrapper);
        setUserInfo(poPage.getRecords());
        return poPage;
    }

    @Override
    public List<ProjectPo> getList(ProjectDaoQuery daoQuery) {
        QueryWrapper<ProjectPo> queryWrapper = new QueryWrapper<>();
        setProjectQueryWrapper(daoQuery, queryWrapper);
        List<ProjectPo> projectPoList = baseMapper.selectList(queryWrapper);
        setUserInfo(projectPoList);
        return projectPoList;
    }

    @Override
    public List<ProjectPo> getUnCloseProjectList(ProjectDaoQuery daoQuery) {
        QueryWrapper<ProjectPo> queryWrapper = new QueryWrapper<>();
        if(ObjectUtils.isEmpty(daoQuery.getYear())){
            queryWrapper.apply("close_date is null");
        }else{
            queryWrapper.apply("(DATE_FORMAT(close_date,'%Y') >= {0} or close_date is null)",daoQuery.getYear());
        }
        setProjectQueryWrapper(daoQuery, queryWrapper);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public BigDecimal getTotalContractAmount() {
        return baseMapper.getTotalContractAmount();
    }

    @Override
    public BigDecimal getTotalConfirmAmount() {
        return baseMapper.getTotalConfirmAmount();
    }

    @Override
    public BigDecimal getTotalReceivableAmount() {
        return baseMapper.getTotalReceivableAmount();
    }

    @Override
    public BigDecimal getTotalReceivedAmount() {
        return baseMapper.getTotalReceivedAmount();
    }

    @Override
    public BigDecimal getTotalNodeCloseAmount() {
        return baseMapper.getTotalNodeCloseAmount();
    }

    @Override
    public BigDecimal getTotalPayAmount() {
        return baseMapper.getTotalPayAmount();
    }

    @Override
    public Long getCount(String projectName){
        QueryWrapper<ProjectPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("project_name",projectName);
        return baseMapper.selectCount(queryWrapper);
    }

    private void setProjectQueryWrapper(ProjectDaoQuery daoQuery, QueryWrapper<ProjectPo> queryWrapper) {
        queryWrapper.like(ObjectUtils.isNotEmpty(daoQuery.getProjectName()),"project_name",daoQuery.getProjectName());
        queryWrapper.eq(ObjectUtils.isNotEmpty(daoQuery.getProjectType()),"project_type_key",daoQuery.getProjectType());
        queryWrapper.eq(ObjectUtils.isNotEmpty(daoQuery.getProjectStatus()),"project_status",daoQuery.getProjectStatus());
        queryWrapper.eq(ObjectUtils.isNotEmpty(daoQuery.getBusinessType()),"business_type",daoQuery.getBusinessType());
        setSort(daoQuery, queryWrapper);
    }
}
